1 00:00:00,240 --> 00:00:01,350 Welcome back. 2 00:00:01,350 --> 00:00:06,480 In this lecture we're going to continue to learn about data stores by understanding what an ordered 3 00:00:06,480 --> 00:00:07,920 data store is. 4 00:00:07,920 --> 00:00:13,710 Sometimes when we create games, we would like to create a universal leaderboard that shows like the 5 00:00:13,710 --> 00:00:19,920 top ten players in a specific category, like top ten with most kills, top ten with the most money, 6 00:00:19,920 --> 00:00:20,880 and so on. 7 00:00:20,880 --> 00:00:26,070 And we can do so by storing player data within an ordered data store. 8 00:00:26,100 --> 00:00:32,340 Now, if you remember inside of our leaderboard handler, we created our money data store by using the 9 00:00:32,340 --> 00:00:34,440 Get Data Store function. 10 00:00:34,440 --> 00:00:39,660 However, the data store that is returned by this function is not an ordered data store, meaning we 11 00:00:39,660 --> 00:00:42,690 are unable to do things like grab the top ten players. 12 00:00:42,690 --> 00:00:48,300 So we need to swap this out with an ordered data store instead of a regular data store. 13 00:00:48,330 --> 00:00:53,280 To do this, there is another function within the data store service called Get Order Data Store. 14 00:00:53,280 --> 00:00:55,410 So I'm going to create a new variable. 15 00:00:55,410 --> 00:00:59,130 I'm going to call this one Ordered Money Data Store. 16 00:00:59,130 --> 00:01:03,780 And that's going to be equal to the data store service get order data store. 17 00:01:03,780 --> 00:01:07,200 And for this one we can just call it like player money. 18 00:01:08,100 --> 00:01:14,280 However, note that this is going to be creating an entirely new data store, and any data store that 19 00:01:14,280 --> 00:01:18,810 was stored in this previous data store will not show up in this ordered one. 20 00:01:18,810 --> 00:01:23,520 So you need to make sure that you plan out what data you would like to store in an ordered data store 21 00:01:23,520 --> 00:01:28,140 before you start scripting your game, because it will be very hard to migrate data later. 22 00:01:28,530 --> 00:01:33,570 Now that we have this new ordered data store, I'm going to copy this and update the references within 23 00:01:33,570 --> 00:01:34,320 my scripts. 24 00:01:34,320 --> 00:01:38,520 So I don't want to get money data from our money data store anymore. 25 00:01:38,520 --> 00:01:41,250 Instead, we're going to be using the Order Data Store. 26 00:01:41,340 --> 00:01:45,900 And we're also going to be using the Order Data Store for saving money data. 27 00:01:45,900 --> 00:01:51,660 And this ordered money data store has the exact same functions as a regular data store, such as update 28 00:01:51,660 --> 00:01:52,590 async. 29 00:01:52,590 --> 00:02:00,720 We also have set async, get async, and then it also has the standard function of remove async. 30 00:02:00,720 --> 00:02:05,640 And I don't believe I talked about this function last time, but this one's very simple. 31 00:02:05,640 --> 00:02:10,230 All you need to do is pass a key in here, and if it finds the key in the table, then it completely 32 00:02:10,230 --> 00:02:13,260 removes that key and its value from the data store. 33 00:02:13,260 --> 00:02:14,700 So that's what that function does. 34 00:02:15,180 --> 00:02:19,920 But this ordered money data store has another function inside of it that we get to use. 35 00:02:20,010 --> 00:02:23,340 And this function is called Get sorted async. 36 00:02:23,340 --> 00:02:26,490 And it says it returns a data store pages object. 37 00:02:26,490 --> 00:02:33,450 So what we do in this function is we specify if we would like to grab, um, data in ascending or descending 38 00:02:33,450 --> 00:02:34,170 order. 39 00:02:34,170 --> 00:02:40,470 In this case, since we want to grab the top ten players, then we need to keep ascending at false. 40 00:02:40,470 --> 00:02:45,240 We can specify the page size or the number of keys that we would like to grab. 41 00:02:45,240 --> 00:02:49,560 So if we would like to grab the top ten players then we would pass the number ten here. 42 00:02:49,650 --> 00:02:55,110 And then there's some other optional data such as a minimum value stored at those keys, and a maximum 43 00:02:55,110 --> 00:02:55,650 value. 44 00:02:55,650 --> 00:03:01,920 So if any value stored does not match this minimum value or any value goes above the max value, then 45 00:03:01,920 --> 00:03:03,300 it's going to ignore that key. 46 00:03:03,300 --> 00:03:07,260 And it's not going to be included in this data store pages object. 47 00:03:08,960 --> 00:03:11,810 So what I'm going to do is I'm going to create a new function. 48 00:03:16,000 --> 00:03:20,620 And I'm going to call this function get top money players. 49 00:03:20,800 --> 00:03:26,980 And I want this function to return to me a data store page that has the top five players. 50 00:03:26,980 --> 00:03:33,700 So what we need to do is we need to refer to our ordered money data store and use the get sorted async 51 00:03:33,700 --> 00:03:34,660 function. 52 00:03:34,660 --> 00:03:37,570 We want to keep it descending. 53 00:03:37,570 --> 00:03:41,020 So we're going to set this false for the ascending boolean. 54 00:03:41,020 --> 00:03:46,750 And then we want to grab let's say the top five players with the most money. 55 00:03:46,750 --> 00:03:51,340 Now since this is an async function, that means we also need to wrap it in a p call. 56 00:03:51,340 --> 00:03:58,630 So we'll do p call, pass a function here, and then we'll control and x this and then return the value 57 00:03:58,630 --> 00:04:05,380 returned from our ordered money data store will store this in the success and then the result variable. 58 00:04:06,940 --> 00:04:12,400 And if we weren't successful in grabbing the top five players, then we'll just warn in the console. 59 00:04:12,580 --> 00:04:15,280 Fail to grab top players. 60 00:04:15,970 --> 00:04:17,440 And I'll just return. 61 00:04:17,440 --> 00:04:22,390 Otherwise we can go ahead and get the top five players from the result. 62 00:04:22,390 --> 00:04:26,320 And since this returns it says a data store pages object. 63 00:04:26,320 --> 00:04:33,190 Let's go ahead and go over to the Roblox API to see what the functions are for a data store pages object. 64 00:04:33,730 --> 00:04:40,660 So here in the Roblox documentation, it has a data store pages and it says a special type of pages 65 00:04:40,660 --> 00:04:45,340 object whose pages contain key value pairs from an ordered data store. 66 00:04:45,340 --> 00:04:52,840 For this object, the function get current page returns an array of tables, each containing keys named 67 00:04:52,840 --> 00:04:54,550 key and value. 68 00:04:54,550 --> 00:04:57,400 These reflect the key value pair data. 69 00:04:58,120 --> 00:04:59,350 So it says right here. 70 00:04:59,350 --> 00:05:00,610 And here's our example. 71 00:05:00,610 --> 00:05:06,070 When we get our data store pages object, we have to use a function called get current page to get. 72 00:05:06,070 --> 00:05:08,350 For example here they're getting a top ten. 73 00:05:08,350 --> 00:05:10,630 And then they loop through this top ten. 74 00:05:10,630 --> 00:05:16,120 It shows the rank or the index as well as the data stored at that rank. 75 00:05:16,120 --> 00:05:21,520 So for example the number one player would have their data and that data would represent the key and 76 00:05:21,520 --> 00:05:22,000 the value. 77 00:05:22,000 --> 00:05:23,800 So the key would be the player's user ID. 78 00:05:23,800 --> 00:05:26,320 And then the value would be how much money they have. 79 00:05:26,320 --> 00:05:30,220 So we can go ahead and implement this into our script. 80 00:05:30,730 --> 00:05:36,700 So once we get this result from our ordered money data store, the function is said to use is get current 81 00:05:36,700 --> 00:05:37,510 page. 82 00:05:37,510 --> 00:05:40,900 And this is going to return to us, our top five players. 83 00:05:40,900 --> 00:05:45,250 And I'm going to go ahead and return these top five players out of our function. 84 00:05:45,400 --> 00:05:51,910 And then what I'm going to do simply for demonstration purposes is I'm going to scroll down and before 85 00:05:51,910 --> 00:05:55,210 our while loop, I'm just going to yield for five seconds. 86 00:05:56,020 --> 00:06:01,300 And then we're going to go ahead and get the result from our Get Top Money players function. 87 00:06:03,000 --> 00:06:08,940 And then we're going to go ahead and loop and get every single rank and data inside of our result. 88 00:06:09,560 --> 00:06:15,980 And from here we can go ahead and print out into the console something like player, and then we can 89 00:06:15,980 --> 00:06:17,570 pass the key. 90 00:06:17,570 --> 00:06:22,280 Remember this data object or this table right here has a key called key. 91 00:06:22,850 --> 00:06:24,920 So this would be our player's user ID. 92 00:06:24,920 --> 00:06:30,740 So player with this user id we can say they are the rank or is rank. 93 00:06:30,740 --> 00:06:34,550 And then we could pass the rank which would be this value right here. 94 00:06:35,450 --> 00:06:37,820 And then we can say how much money they have. 95 00:06:37,820 --> 00:06:44,540 So player with this user ID is rank let's say rank number one with how much money do they have. 96 00:06:44,540 --> 00:06:47,870 And then we could pass the data dot value. 97 00:06:48,440 --> 00:06:51,740 And then we could say they have this many dollars. 98 00:06:52,810 --> 00:06:59,380 So five seconds after I joined the game, it should grab the top five players inside of our data store 99 00:06:59,380 --> 00:07:02,500 and then it'll print into the console the top five players. 100 00:07:02,500 --> 00:07:08,500 Since this is a newly created data store, there shouldn't be any data stored inside of here, meaning 101 00:07:08,500 --> 00:07:09,520 this shouldn't print. 102 00:07:09,520 --> 00:07:12,430 So what I need to do is I need to join the game. 103 00:07:12,430 --> 00:07:18,460 I need to set some money for my player, leave the game and save that data and then rejoin again. 104 00:07:18,940 --> 00:07:21,520 So let's go ahead and play test the game. 105 00:07:24,390 --> 00:07:29,220 And since we're using that new data store, of course I have zero money, but I'm going to go ahead 106 00:07:29,220 --> 00:07:31,620 and update the value inside of here. 107 00:07:31,620 --> 00:07:33,690 We could set it to something like 1000. 108 00:07:34,760 --> 00:07:37,760 And then I can go ahead and leave the game to save that data. 109 00:07:40,400 --> 00:07:43,580 And now I can go back and play my game again. 110 00:07:45,330 --> 00:07:49,410 Here we have my $1,000 and it should print into the console. 111 00:07:49,440 --> 00:07:50,370 There we go. 112 00:07:50,580 --> 00:07:52,140 Player there is. 113 00:07:52,140 --> 00:07:56,670 My user ID is ranked number one with $1,000. 114 00:07:57,090 --> 00:08:02,820 And just like that, we have successfully grabbed the top players in our game with the most money. 115 00:08:03,750 --> 00:08:06,420 Now you can do a lot with this data. 116 00:08:06,420 --> 00:08:13,200 You could update a surface UI that shows all the top players in your game with the most money. 117 00:08:13,200 --> 00:08:15,540 There's a whole bunch of different stuff you can do with it. 118 00:08:15,540 --> 00:08:18,510 It's really up to your own creative preference. 119 00:08:19,170 --> 00:08:23,100 Otherwise that's how simple it is to use ordered data stores. 120 00:08:23,100 --> 00:08:24,630 Not much more than that. 121 00:08:24,870 --> 00:08:28,230 Keep up the good work and I'll see you in the next one.